export default class ImageInfo {
    version="5.4.1";
    flags={};
    shapes=[];
    imagePath="";
    imageData="";
    imageHeight=0;
    imageWidth=0;
    constructor(imagePath) {
        this.imagePath=imagePath;
    }

    /**
     * 导入JSON数据
     * @param json
     */
    loadFromJson(json) {
        this.version=json.version;
        this.flags=json.flags;
        this.shapes=json.shapes.map(shape => {
            if(shape.shape_type=="rectangle"){
                shape={
                    ...shape,
                    points: {
                        x: shape.points[0][0],
                        y: shape.points[0][1],
                        width: shape.points[1][0]-shape.points[0][0],
                        height: shape.points[1][1]-shape.points[0][1]
                    }
                }
            }else if(shape.shape_type=="circle"){
                shape={
                    ...shape,
                    points: {
                        x: shape.points[0][0],
                        y: shape.points[0][1],
                        radius: Math.sqrt(Math.pow(shape.points[1][0]-shape.points[0][0], 2) + Math.pow(shape.points[1][1]-shape.points[0][1], 2))
                    }
                }
            }else if(shape.shape_type=="polygon"){
                shape={
                    ...shape,
                    points: shape.points.map(point => {
                        return {x: point[0], y: point[1]};
                    })
                }
            }else if(shape.shape_type=="line"){
                shape={
                    ...shape,
                    points: shape.points.map(point => {
                        return {x: point[0], y: point[1]};
                    })
                }
            }else if(shape.shape_type=="point"){
                shape={
                    ...shape,
                    points: {
                        x: shape.points[0][0],
                        y: shape.points[0][1]
                    }
                }
            }else if(shape.shape_type=="text"){
                shape={
                    ...shape,
                    points: {
                        x: shape.points[0][0],
                        y: shape.points[0][1],
                        text: shape.text
                    }
                }
            }else if(shape.shape_type=="arrow"){
                shape={
                    ...shape,
                    points: shape.points.map(point => {
                        return {x: point[0], y: point[1]};
                    })
                }
            }
            return shape;
        });
        this.imagePath=json.imagePath;
        this.imageData=json.imageData;
        this.imageHeight=json.imageHeight;
        this.imageWidth=json.imageWidth;
    }

    /**
     * 导出JSON数据
     * @returns {{}}
     */
    exportJSON() {
        return {
            version: this.version,
            flags: this.flags,
            shapes: this.shapes.map(shape => {
                shape.points=shape.points.map(point => {
                    return [point.x, point.y];
                });
                return shape;
            }),
            imagePath: this.imagePath,
            imageData: this.imageData,
            imageHeight: this.imageHeight,
            imageWidth: this.imageWidth
        };
    }

    /**
     * 获取图片路径
     * @returns {string}
     */
    getImagePath() {
        return this.imagePath;
    }
/**
     * 设置图片路径
     * @param imagePath
     */
    setImagePath(imagePath) {
        this.imagePath=imagePath;
    }

    /**
     * 获取图片数据
     * @returns {string}
     */
    getImageData() {
        return this.imageData;
    }

    /**
     * 设置图片数据
     * @param imageData
     */
    setImageData(imageData) {
        this.imageData=imageData;
    }

    /**
     * 获取图片高度
     * @returns {number}
     */
    getImageHeight() {
        return this.imageHeight;
    }

    /**
     * 设置图片高度
     * @param imageHeight
     */
    setImageHeight(imageHeight) {
        this.imageHeight=imageHeight;
    }

    /**
     * 获取图片宽度
     * @returns {number}
     */
    getImageWidth() {
        return this.imageWidth;
    }

    /**
     * 设置图片宽度
     * @param imageWidth
     */
    setImageWidth(imageWidth) {
        this.imageWidth=imageWidth;
    }

    /**
     * 获取标志位
     * @param flag
     * @returns {boolean}
     */
    getFlag(flag) {
        return this.flags[flag];
    }

    /**
     * 设置标志位
     * @param flag
     * @param value
     */
    setFlag(flag, value) {
        this.flags[flag]=value;
    }

    /**
     * 获取所有形状
     * @returns {[]}
     */
    getShapes() {
        return this.shapes;
    }

    /**
     * 添加形状
     * @param shape
     */
    addShape(shape) {
        this.shapes.push(shape);
    }

    /**
     * 删除形状
     * @param shape
     */
    removeShape(shape) {
        const index=this.shapes.indexOf(shape);
        if (index>=0) {
            this.shapes.splice(index, 1);
        }
    }

    /**
     * 清空所有形状
     */
    clearShapes() {
        this.shapes=[];
    }


}